/**
 * 
 * 
 *  *2010-3-22 下午09:30:53  add 
 */
package com.base.myproject.client.busi.ccode;

import com.base.myproject.client.system.BaseResource;
import com.base.myproject.client.system.Env;
import com.base.myproject.client.tools.CommandSyncContainer;
import com.base.myproject.client.tools.CommandSyncReturnObject;
import com.base.myproject.client.tools.CommandSyncsql;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.Html;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.DeferredCommand;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Image;

/**
 * 根据用户的卡号进行密码修改
 * ccode，的用户密码修改。
 * 需要用户录入久密码，和2次新密码
 * 再每一项录入回车后，方可继续操作。
 * @author Administrator 2010-3-22 javazgw@gmail.com
 */
public class CcodeChangePass {

	String s_cardno;
	TextField<String> oldpass;
	TextField<String> pass;
	TextField<String> Confirmpass;
	 TextField<String> s_cardnoT = new TextField<String>();
	 Dialog d = new Dialog();
	Image image = new Image(BaseResource.INSTANCE.clear());
	 FormPanel formpanel = new FormPanel();

	public CcodeChangePass(String s_cardno) {
		this.s_cardno = s_cardno;
	}

	public void showDialog() {

		d.setWidth("100");
		d.setTitle("修改密码");
		d.setHeading("修改密码");
		FormData fd = new FormData("-20");
//		formpanel.setLabelWidth(75);
//		formpanel.setFieldWidth(100);
		formpanel.setHeaderVisible(false);
		formpanel.setBodyBorder(false);
		formpanel.setBorders(false);
		
		s_cardnoT.setValue(Env.getEnvname());
		s_cardnoT.setFieldLabel("会员卡号");
		s_cardnoT.setEmptyText("请录入会员卡号");
		if(s_cardno!=null)
			s_cardnoT.setEnabled(false);
		s_cardnoT.setAllowBlank(false);
		s_cardnoT.setValue(s_cardno);

		oldpass = new TextField<String>();
//		oldpass.setAllowBlank(false);
		oldpass.setFieldLabel("旧密码");
		oldpass.setEmptyText("录入后请按回车键");
		
		oldpass.setPassword(true);

		pass = new TextField<String>();
		pass.setAllowBlank(false);
		pass.setEmptyText("录入后请按回车键");
		pass.setFieldLabel("密码");
		pass.setPassword(true);
		pass.setEnabled(false);
		Confirmpass = new TextField<String>();
		Confirmpass.setAllowBlank(false);
		Confirmpass.setFieldLabel("确认密码");
		Confirmpass.setEmptyText("录入后请按回车键");
		Confirmpass.setPassword(true);
		Confirmpass.setEnabled(false);
		
		
		formpanel.add(s_cardnoT,fd);
		formpanel.add(oldpass,fd);
		formpanel.add(image,fd);
		formpanel.add(pass,fd);
		formpanel.add(Confirmpass,fd);
		
		formpanel.add(new Html("操作提示：请确认录入每一项后按回车键，继续下一步"),fd);


		d.setSize(360, 300);
		//d.setAutoHeight(true);
		//d.setAutoWidth(true);
		d.add(formpanel);
		d.setButtons(Dialog.OKCANCEL);

		d.getButtonById(Dialog.CANCEL)
				.addListener(Events.Select, getListener());

		d.getButtonById(Dialog.OK).addListener(Events.Select, getListener());
		
		oldpass.addListener(Events.KeyUp, getListener());
		pass.addListener(Events.KeyUp, getListener());
		Confirmpass.addListener(Events.KeyUp, getListener());
		s_cardnoT.addListener(Events.KeyUp, getListener());
		d.getButtonById(Dialog.OK).setEnabled(false);
		d.show();

	}

	int sync = 0;
	Listener<BaseEvent> defaultlistener;

	Listener<BaseEvent> getListener() {
		if (defaultlistener != null) {
			return defaultlistener;
		}

		defaultlistener = new Listener<BaseEvent>() {
			public void handleEvent(BaseEvent be) {
				sync++;
				try {
					if (sync == 1) {
						if (be.getType() == Events.Select) {
							if (be.getSource().equals(
									d.getButtonById(Dialog.OK))) {
								d.getButtonById(Dialog.OK).setEnabled(false);
								Confirmpass.clearInvalid();
								if(!Confirmpass.getValue().equals(pass.getValue()))
								{
									Confirmpass.markInvalid("两次录入密码不一致");
									
									
								}
								else if(formpanel.isValid())
								{
									
									CommandSyncContainer list = new CommandSyncContainer();
									CommandSyncsql commandsql = new CommandSyncsql();
								
									commandsql.getV().add("update ccode set [password]='"+pass.getValue()+"' where s_cardno='"+s_cardnoT.getValue()+"'");
									list.add(commandsql);
									final com.base.myproject.client.tools.GreetingServiceAsync greetingService = GWT
									.create(com.base.myproject.client.tools.GreetingService.class);
									greetingService.SendCommandSync(list,new AsyncCallback<CommandSyncReturnObject>(){

										@Override
										public void onFailure(Throwable caught) {
											MessageBox.alert("错误", "网络连接失败", null);
											d.getButtonById(Dialog.OK).setEnabled(true);
										}

										@Override
										public void onSuccess(
												CommandSyncReturnObject result) {
											if(result.isB())
											{
												Info.display("", "更新成功");
												d.setVisible(false);
												formpanel = null;
												d = null;
											}
											else{
												MessageBox.alert("错误", "更新失败，错误未知！"+result.getMessage(), null);
												d.getButtonById(Dialog.OK).setEnabled(true);
											}
											
										}
										
									});
									
								}

							} else if (be.getSource().equals(
									d.getButtonById(Dialog.CANCEL))) {
								d.setVisible(false);
								formpanel = null;
								d = null;
							}

						} else if (be.getType() == Events.Hide) {

						} else if (be.getType() == Events.Change) {
							if (be.getSource().equals("")) {

							}
						} else if (be.getType() == Events.KeyUp) {
							if (be.getSource().equals(oldpass)) {
								if (be instanceof ComponentEvent) {
									if (((ComponentEvent) be).getKeyCode() == 13) // 回车
									{
										Command cmd = new Command()
										{
										      public void execute()
										      {
										    	  
										    	  if(s_cardnoT.getValue()!=null)
													{
										    		  final com.base.myproject.client.tools.GreetingServiceAsync greetingService = GWT
														.create(com.base.myproject.client.tools.GreetingService.class);
														greetingService.checkhasCcode_scardno(s_cardnoT.getValue(), oldpass.getValue(), new AsyncCallback<Boolean>(){

															@Override
															public void onFailure(
																	Throwable caught) {
															
																
															}

															@Override
															public void onSuccess(
																	Boolean result) {
																if(result)
																{
																	image.setResource(BaseResource.INSTANCE.checkmark_48());
																
																	Confirmpass.setEnabled(true);
																	pass.setEnabled(true);
																	pass.focus();
																}
																else
																{
																	image.setResource(BaseResource.INSTANCE.delete_48());	
																}
															}
															
														});
													
													}
										    	  else {
										    		  MessageBox.alert("错误", "请先录入会员卡号", null);
										    	  }
										    	  
										    	 
										      }
										};

										DeferredCommand.addCommand(cmd);
									
									}
								}
							}
							else if (be.getSource().equals(pass)) {
								if (be instanceof ComponentEvent) {
									if (((ComponentEvent) be).getKeyCode() == 13) // 回车
									{
										Command cmd = new Command()
										{
										      public void execute()
										      {
										    	  Confirmpass.focus();
										      }
										};

										DeferredCommand.addCommand(cmd);
										
										
									}
								}
							}
							else if (be.getSource().equals(s_cardnoT)) {
								if (be instanceof ComponentEvent) {
									if (((ComponentEvent) be).getKeyCode() == 13) // 回车
									{
										Command cmd = new Command()
										{
										      public void execute()
										      {
										    	  oldpass.focus();
										      }
										};

										DeferredCommand.addCommand(cmd);
										
										
									}
								}
							}
							else if (be.getSource().equals(Confirmpass)) {
								if (be instanceof ComponentEvent) {
									if (((ComponentEvent) be).getKeyCode() == 13) // 回车
									{
										Command cmd = new Command()
										{
										      public void execute()
										      {
										    	  Confirmpass.clearInvalid();
													if(!Confirmpass.getValue().equals(pass.getValue()))
													{
														Confirmpass.markInvalid("两次录入密码不一致");
														
														
													}
													else
													{
														d.getButtonById(Dialog.OK).setEnabled(true);
														d.getButtonById(Dialog.OK).focus();
													}
										      }
										};

										DeferredCommand.addCommand(cmd);
										
									}
								}
							}
						}
					}
				} finally {
					sync--;
				}
			}

		};
		return defaultlistener;
	}
}
